package myservice.android.activities;

import java.util.Calendar;

import myservice.android.Global;
import myservice.android.R;
import myservice.android.database.Database;
import android.app.Activity;
import android.database.Cursor;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class PickDateActivity extends Activity {
	
	private Button previousMonthButton, nextMonthButton;
	private TextView windowTitle;	
	private TextView currentMonthText;
	private LinearLayout topBar, bottomBar;
	
	private Button[][] calendarDays = new Button[6][7];
	private TextView[] dayLabels = new TextView[7];
	private Calendar calendar;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {    	
        super.onCreate(savedInstanceState);
        
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.calendar);
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.titlebar);
        windowTitle = (TextView) findViewById(R.id.titleText);                      	    	
                
        windowTitle.setText(Global.res.getString(R.string.title_select_date));        
        
        currentMonthText = (TextView) findViewById(R.id.selectedMonth);
        previousMonthButton = (Button) findViewById(R.id.previousButton);
        nextMonthButton = (Button) findViewById(R.id.nextButton);  
        topBar = (LinearLayout) findViewById(R.id.topBar);
        bottomBar = (LinearLayout) findViewById(R.id.bottomBar); 
        
        topBar.setVisibility(View.GONE);
        bottomBar.setVisibility(View.GONE);
        
        calendar = Calendar.getInstance();
        
        // Retrieve day names above week days
        dayLabels[0] = (TextView) findViewById(R.id.dayLabel0);
        dayLabels[1] = (TextView) findViewById(R.id.dayLabel1);
        dayLabels[2] = (TextView) findViewById(R.id.dayLabel2);
        dayLabels[3] = (TextView) findViewById(R.id.dayLabel3);
        dayLabels[4] = (TextView) findViewById(R.id.dayLabel4);
        dayLabels[5] = (TextView) findViewById(R.id.dayLabel5);
        dayLabels[6] = (TextView) findViewById(R.id.dayLabel6);
        
        // Retrieve calendar days
        calendarDays[0][0] = (Button) findViewById(R.id.button0_0);
        calendarDays[0][1] = (Button) findViewById(R.id.button0_1);
        calendarDays[0][2] = (Button) findViewById(R.id.button0_2);
        calendarDays[0][3] = (Button) findViewById(R.id.button0_3);
        calendarDays[0][4] = (Button) findViewById(R.id.button0_4);
        calendarDays[0][5] = (Button) findViewById(R.id.button0_5);        
        calendarDays[0][6] = (Button) findViewById(R.id.button0_6);                
        calendarDays[1][0] = (Button) findViewById(R.id.button1_0);
        calendarDays[1][1] = (Button) findViewById(R.id.button1_1);
        calendarDays[1][2] = (Button) findViewById(R.id.button1_2);
        calendarDays[1][3] = (Button) findViewById(R.id.button1_3);
        calendarDays[1][4] = (Button) findViewById(R.id.button1_4);
        calendarDays[1][5] = (Button) findViewById(R.id.button1_5);        
        calendarDays[1][6] = (Button) findViewById(R.id.button1_6);              
        calendarDays[2][0] = (Button) findViewById(R.id.button2_0);
        calendarDays[2][1] = (Button) findViewById(R.id.button2_1);
        calendarDays[2][2] = (Button) findViewById(R.id.button2_2);
        calendarDays[2][3] = (Button) findViewById(R.id.button2_3);
        calendarDays[2][4] = (Button) findViewById(R.id.button2_4);
        calendarDays[2][5] = (Button) findViewById(R.id.button2_5);        
        calendarDays[2][6] = (Button) findViewById(R.id.button2_6);           
        calendarDays[3][0] = (Button) findViewById(R.id.button3_0);
        calendarDays[3][1] = (Button) findViewById(R.id.button3_1);
        calendarDays[3][2] = (Button) findViewById(R.id.button3_2);
        calendarDays[3][3] = (Button) findViewById(R.id.button3_3);
        calendarDays[3][4] = (Button) findViewById(R.id.button3_4);
        calendarDays[3][5] = (Button) findViewById(R.id.button3_5);        
        calendarDays[3][6] = (Button) findViewById(R.id.button3_6);        
        calendarDays[4][0] = (Button) findViewById(R.id.button4_0);
        calendarDays[4][1] = (Button) findViewById(R.id.button4_1);
        calendarDays[4][2] = (Button) findViewById(R.id.button4_2);
        calendarDays[4][3] = (Button) findViewById(R.id.button4_3);
        calendarDays[4][4] = (Button) findViewById(R.id.button4_4);
        calendarDays[4][5] = (Button) findViewById(R.id.button4_5);        
        calendarDays[4][6] = (Button) findViewById(R.id.button4_6);        
        calendarDays[5][0] = (Button) findViewById(R.id.button5_0);
        calendarDays[5][1] = (Button) findViewById(R.id.button5_1);
        calendarDays[5][2] = (Button) findViewById(R.id.button5_2);
        calendarDays[5][3] = (Button) findViewById(R.id.button5_3);
        calendarDays[5][4] = (Button) findViewById(R.id.button5_4);
        calendarDays[5][5] = (Button) findViewById(R.id.button5_5);        
        calendarDays[5][6] = (Button) findViewById(R.id.button5_6);               
        
		for (int i=0; i<6; i++) {
			for (int j=0; j<7; j++) {
				calendarDays[i][j].setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View button) {
						Button b = (Button) button;
						
						int date = Integer.parseInt(b.getText().toString());
						int month = calendar.get(Calendar.MONTH);
						int year = calendar.get(Calendar.YEAR);											

						Global.selectedId=1; // dummy value just to be different from 0
						Global.selectedType=2;
						Global.selectedDate=date;
						Global.selectedMonth=month;
						Global.selectedYear=year;
						finish();
						
					}
					
				});
			}
		}
		
		previousMonthButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				switchMonth(-1);				
			}			
		});
		
		nextMonthButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				switchMonth(1);				
			}			
		});		
        
    }
    
    @Override
    public void onResume() {
    	super.onResume();
        updateCalendar();
    }    
	
	private void switchMonth(int direction) {
		int year = calendar.get(Calendar.YEAR);
		int month = calendar.get(Calendar.MONTH) + direction;
		
		if (month<0) {
			month=11; year--;
		}
		
		if (month>11) {
			month=0; year++;
		}
		
		calendar.set(Calendar.YEAR, year);
		calendar.set(Calendar.MONTH, month);
		
		updateCalendar();
		
	}
	
	/** Updates data in the calendar */
	private void updateCalendar() {
		
		currentMonthText.setText(Global.monthNames[calendar.get(Calendar.MONTH)]+" "+calendar.get(Calendar.YEAR));		
		
		// Update weekday labels
		if (Global.firstDayOfWeek==0) {
			dayLabels[0].setText(Global.res.getText(R.string.label_sun));
			dayLabels[1].setText(Global.res.getText(R.string.label_mon));
			dayLabels[2].setText(Global.res.getText(R.string.label_tue));
			dayLabels[3].setText(Global.res.getText(R.string.label_wed));
			dayLabels[4].setText(Global.res.getText(R.string.label_thu));
			dayLabels[5].setText(Global.res.getText(R.string.label_fri));
			dayLabels[6].setText(Global.res.getText(R.string.label_sat));
		} else {
			dayLabels[0].setText(Global.res.getText(R.string.label_mon));
			dayLabels[1].setText(Global.res.getText(R.string.label_tue));
			dayLabels[2].setText(Global.res.getText(R.string.label_wed));
			dayLabels[3].setText(Global.res.getText(R.string.label_thu));
			dayLabels[4].setText(Global.res.getText(R.string.label_fri));
			dayLabels[5].setText(Global.res.getText(R.string.label_sat));
			dayLabels[6].setText(Global.res.getText(R.string.label_sun));
		}
		
		for (int i=0; i<6; i++) {
			for (int j=0; j<7; j++) {
				calendarDays[i][j].setVisibility(View.INVISIBLE);
				calendarDays[i][j].getBackground().clearColorFilter();
			}
		}
		
		// Populate calendar buttons with dates
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.YEAR, calendar.get(Calendar.YEAR));
		cal.set(Calendar.MONTH, calendar.get(Calendar.MONTH));
		cal.set(Calendar.DAY_OF_MONTH, 1);
		int daysInMonth = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
		int row=0;
		int col = cal.get(Calendar.DAY_OF_WEEK)-1;
		if (Global.firstDayOfWeek==1)
			col--;
		if (col<0)
			col=6;
		int i=1;
		while (i<=daysInMonth) {
			calendarDays[row][col].setVisibility(View.VISIBLE);
			calendarDays[row][col].setText(""+i);
			calendarDays[row][col].setTypeface(Typeface.DEFAULT);
			calendarDays[row][col].setTextSize(13);
			
			Calendar today = Calendar.getInstance();
			
			// Highlight today's date
			if (i==today.get(Calendar.DAY_OF_MONTH) && today.get(Calendar.MONTH)==cal.get(Calendar.MONTH) && today.get(Calendar.YEAR)==cal.get(Calendar.YEAR))
				calendarDays[row][col].getBackground().setColorFilter(0xFFFFD700, PorterDuff.Mode.MULTIPLY);

			i++;
			col++;
			if (col>6) {
				col=0;
				row++;
			}
		}
		
		// Highlight days that have some ministry data recorded
		Cursor cursor;		
		cursor = Database.select("SELECT date FROM ministry WHERE year="+cal.get(Calendar.YEAR)+" AND month="+cal.get(Calendar.MONTH)+
				                        " AND (hours>0 OR minutes>0 OR chours>0 OR cminutes>0 OR magazines>0 OR brochures>0 OR books>0 OR returncalls>0 OR comments!='')");
		while (cursor.moveToNext()) {
			int d = cursor.getInt(cursor.getColumnIndex("date"));
			for (int r=0; r<6; r++)
				for (int c=0; c<7; c++)
					if (calendarDays[r][c].getText().equals(""+d) && calendarDays[r][c].getVisibility()==View.VISIBLE) {
						calendarDays[r][c].setTypeface(null, Typeface.BOLD);
						calendarDays[r][c].setTextSize(16);
						break;
					}
		}
		cursor.close();		
		
		// Highlight days that have calls to persons recorded
		cursor = Database.select("SELECT date FROM calls WHERE year="+cal.get(Calendar.YEAR)+" AND month="+cal.get(Calendar.MONTH)+" AND personid<>0 AND timestamp<>"+Global.TIMESTAMP_DELETED);
		while (cursor.moveToNext()) {
			int d = cursor.getInt(cursor.getColumnIndex("date"));
			for (int r=0; r<6; r++)
				for (int c=0; c<7; c++)
					if (calendarDays[r][c].getText().equals(""+d) && calendarDays[r][c].getVisibility()==View.VISIBLE) {
						calendarDays[r][c].setTypeface(null, Typeface.BOLD);
						calendarDays[r][c].setTextSize(16);
						calendarDays[r][c].setTextColor(Color.parseColor("#ffaa00"));						
						break;
					}
		}
		cursor.close();			
		
	}	
    
}